<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加照片</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/admin/layuimini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/layuimini/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">照片url</label>
        <div class="layui-input-block">
            <input type="url" name="url" lay-verify="required|url" lay-reqtext="照片url不能为空" placeholder="请输入照片url"
                   value=""
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label required">简介</label>
        <div class="layui-input-block">
            <textarea name="description" lay-verify="required|description" lay-reqtext="描述不能为空" placeholder="请输入描述"
                      class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">菜单排序</label>
        <div class="layui-input-block">
            <input type="number" name="sort" placeholder="请输入排序值(1~1024)" value="" class="layui-input"
                   lay-verify="number|sort">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">前台显示</label>
        <div class="layui-input-block">
            <input type="checkbox" name="display" lay-skin="switch" lay-text="显示|隐藏" value="true" checked>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveBtn">确认保存</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script src="/static/admin/layuimini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.js"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //自定义验证规则
        form.verify({
            description: function (value) {
                if (value.length > 30) {
                    return '描述长度在30个字符以内';
                }
            },
            //验证排序值
            sort: function (value) {
                if (value < 1 || value > 1024) {
                    return '排序值在1-1024之间'
                }
            },
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            axios({
                method: 'post',
                url: '/admin/photo',
                data: data.field,
                headers: {
                    'X-Requested-With': 'XMLHttpRequest'
                },
            }).then((response) => {
                Swal.fire({
                    icon: 'success',
                    title: '保存成功',
                    showConfirmButton: false,
                    timer: 1500,
                    onClose: () => {
                        var parentLayer = parent.layer;
                        var iframeIndex = parentLayer.getFrameIndex(window.name);
                        parent.location.reload();
                        parentLayer.close(iframeIndex);
                    }
                });
            }).catch((error) => {
                Swal.fire({
                    icon: 'error',
                    title: '保存失败！',
                    text: `${error.response.data.message}`,
                });
            })
            return false;
        });

    });
</script>
</body>
</html>